Daily Profiles - Decompose
## [1] "English_United States.1252"
library(plotly)
library(dplyr)
library(lubridate)
# load time series data
df <- read.csv("https://github.com/hslu-ige-laes/edar/raw/master/sampleData/eboBookEleMeter.csv",
stringsAsFactors=FALSE,
sep =";")
# rename column names
colnames(df) <- c("timestamp", "meterValue")
df$timestamp <- parse_date_time(df$timestamp,
orders = "YmdHMS",
tz = "Europe/Zurich")
df$timestamp <- force_tz(df$timestamp, tzone = "UTC")
# uncomment to filter time range if necessary
#df <- df %>% filter(timestamp > "2015-03-01 00:00:00", timestamp < "2015-04-01 00:00:00")
# Fill missing values with NA
grid.df <- data.frame(timestamp = seq(min(df$timestamp, na.rm = TRUE),
max(df$timestamp, na.rm = TRUE),
by = "15 mins"))
df <- merge(df, grid.df, all = TRUE)
# convert steadily counting energy meter value from kWh to power in kW
df <- df %>%
mutate(value = (meterValue - lag(meterValue))*4) %>%
select(-meterValue) %>%
na.omit()
# remove negative values which occur beause of change summer/winter time
df <- df %>% filter(value >= 0)
# add metadata for later grouping and visualization purposes
df$x <- hour(df$timestamp) + minute(df$timestamp)/60 + second(df$timestamp) / 3600
df$weekday <- weekdays(df$timestamp)
df$weekday <- factor(df$weekday, c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday"))
df <- df %>% mutate(value = ifelse(x == 0.00, NA, df$value))
# Calculate Mean value for all 15 minutes for each weekday
df <- df %>% group_by(weekday, x) %>% mutate(dayTimeMean = mean(value)) %>% ungroup()
# shrink data frame
df <- df %>%
select(x, weekday, timestamp, dayTimeMean) %>%
unique() %>%
na.omit() %>%
arrange(weekday, x)
# plot graph with mean values
maxValMean <- max(df$dayTimeMean, na.rm = TRUE)*1.05
df %>%
highlight_key(~weekday) %>%
plot_ly(x=~x,
y=~dayTimeMean,
color=~weekday,
type="scatter",
mode="lines",
alpha = 0.25,
colors = "dodgerblue4",
text = ~weekday,
hovertemplate = paste("Time: ", format(df$timestamp, "%H:%M"),
"<br>Mean: %{y:.0f}")) %>%
# workaround with add_trace to have fixed y axis when selecting a dedicated day
add_trace(x = 0, y = 0, type = "scatter", showlegend = FALSE, opacity=0) %>%
add_trace(x = 24, y = maxValMean, type = "scatter", showlegend = FALSE, opacity=0) %>%
layout(title = "Daily Profiles - Mean",
showlegend = TRUE,
xaxis = list(
title = "Hour of day",
tickvals = list(0, 3, 6, 9, 12, 15, 18, 21)
),
yaxis = list(
title = "Power (kW)",
range = c(0, maxValMean)
)
) %>%
highlight(on = "plotly_hover",
off = "plotly_doubleclick",
color = "orange",
opacityDim = 0.7,
selected = attrs_selected(showlegend = FALSE)) %>% # this hides elements in the legend
plotly::config(modeBarButtons = list(list("toImage")), displaylogo = FALSE)